<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>魔法方法1 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter05.html" />
    
    
    <link rel="prev" href="./chapter03.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="4"
        data-chapter-title="魔法方法1"
        data-filepath="chapter04.md"
        data-basepath="."
        data-revision="Wed Feb 19 2020 00:43:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        类和对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        面向对象基本语法
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        魔法方法1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        魔法方法2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        内置属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        类属性和对象属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="./chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        私有属性和方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;">&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;</h2>
<p>Python &#x91CC;&#x6709;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#xFF0C;&#x53EB;&#x505A;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;&#x3002;Python &#x7684;&#x7C7B;&#x91CC;&#x63D0;&#x4F9B;&#x7684;&#xFF0C;&#x4E24;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;&#x5F00;&#x59CB;&#xFF0C;&#x4E24;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;&#x7ED3;&#x675F;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5C31;&#x662F;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;&#xFF0C;&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;&#x5728;&#x6070;&#x5F53;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x88AB;&#x6FC0;&#x6D3B;&#xFF0C;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x3002;
&#x9B54;&#x6CD5;&#x65B9;&#x6CD5;&#x7684;&#x4E24;&#x4E2A;&#x7279;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x4E24;&#x4FA7;&#x5404;&#x6709;&#x4E24;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;&#xFF1B;</li>
<li>&quot;&#x5492;&#x8BED;&quot;&#x540D;&#x5B57;&#x5DF2;&#x7ECF;&#x7531; Python &#x5B98;&#x65B9;&#x5B9A;&#x4E49;&#x597D;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x4E71;&#x5199;&#x3002;</li>
</ul>
<h3 id="1init&#x65B9;&#x6CD5;">1.__init__&#x65B9;&#x6CD5;</h3>
<p><code>__init__()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#x9ED8;&#x8BA4;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x624B;&#x52A8;&#x8C03;&#x7528;&#x3002;&#x5728;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x5728;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x5C31;&#x8BBE;&#x7F6E;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x5BF9; <code>__init__</code> &#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x6539;&#x9020;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x732B;&#x7C7B;&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name)</span>:</span>  <span class="hljs-comment"># &#x91CD;&#x5199;&#x4E86; __init__ &#x9B54;&#x6CD5;&#x65B9;&#x6CD5;</span>
        self.name = name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;%s&#x7231;&#x5403;&#x9C7C;&quot;</span>%self.name
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">drink</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;%s&#x7231;&#x559D;&#x6C34;&apos;</span>%self.name

    <span class="hljs-string">&quot;&quot;&quot;
        tom = Cat()
        TypeError: __init__() missing 1 required positional argument: &apos;name&apos;
        &#x8FD9;&#x79CD;&#x5199;&#x6CD5;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x76F4;&#x63A5;&#x62A5;&#x9519;&#xFF01;&#x56E0;&#x4E3A; __init__ &#x65B9;&#x6CD5;&#x91CC;&#x8981;&#x6C42;&#x5728;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x4F20;&#x9012; name &#x5C5E;&#x6027;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x4F20;&#x5165;&#x4F1A;&#x76F4;&#x63A5;&#x62A5;&#x9519;&#xFF01;
    &quot;&quot;&quot;</span>

tom = Cat(<span class="hljs-string">&quot;Tom&quot;</span>)  <span class="hljs-comment"># &#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x6307;&#x5B9A;name&#x5C5E;&#x6027;&#x7684;&#x503C;</span>
tom.eat()   <span class="hljs-comment"># tom&#x7231;&#x5403;&#x9C7C;</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;</p>
<ol>
<li><code>__init__()</code>&#x65B9;&#x6CD5;&#x5728;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F1A;&#x9ED8;&#x8BA4;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x624B;&#x52A8;&#x7684;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</li>
<li><code>__init__()</code>&#x65B9;&#x6CD5;&#x91CC;&#x7684;self&#x53C2;&#x6570;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;python&#x89E3;&#x91CA;&#x5668;&#x4F1A;&#x628A;&#x521B;&#x5EFA;&#x597D;&#x7684;&#x5BF9;&#x8C61;&#x5F15;&#x7528;&#x76F4;&#x63A5;&#x8D4B;&#x503C;&#x7ED9;self</li>
<li>&#x5728;&#x7C7B;&#x7684;&#x5185;&#x90E8;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;self&#x6765;&#x4F7F;&#x7528;&#x5C5E;&#x6027;&#x548C;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#xFF1B;&#x5728;&#x7C7B;&#x7684;&#x5916;&#x90E8;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x540D;&#x6765;&#x4F7F;&#x7528;&#x5C5E;&#x6027;&#x548C;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x3002;</li>
<li>&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x6BCF;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x662F;&#x5404;&#x81EA;&#x4FDD;&#x5B58;&#x7684;&#xFF0C;&#x90FD;&#x6709;&#x5404;&#x81EA;&#x72EC;&#x7ACB;&#x7684;&#x5730;&#x5740;&#x3002;</li>
<li>&#x65B9;&#x6CD5;&#x662F;&#x6240;&#x6709;&#x5BF9;&#x8C61;&#x5171;&#x4EAB;&#x7684;&#xFF0C;&#x53EA;&#x5360;&#x7528;&#x4E00;&#x4EFD;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#xFF0C;&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#x4F1A;&#x901A;&#x8FC7;self&#x6765;&#x5224;&#x65AD;&#x662F;&#x54EA;&#x4E2A;&#x5BF9;&#x8C61;&#x8C03;&#x7528;&#x4E86;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x3002;</li>
</ol>
<h3 id="2del&#x65B9;&#x6CD5;">2.__del__&#x65B9;&#x6CD5;</h3>
<p>&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x540E;&#xFF0C;python&#x89E3;&#x91CA;&#x5668;&#x9ED8;&#x8BA4;&#x8C03;&#x7528;<code>__init__()</code>&#x65B9;&#x6CD5;&#xFF1B;</p>
<p>&#x800C;&#x5F53;&#x5220;&#x9664;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;python&#x89E3;&#x91CA;&#x5668;&#x4E5F;&#x4F1A;&#x9ED8;&#x8BA4;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4E3A;<code>__del__()</code>&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,score)</span>:</span>
        print(<span class="hljs-string">&apos;__init__&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x4E86;&apos;</span>)
        self.name = name
        self.score = score

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__del__</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;__del__&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x4E86;&apos;</span>)
s = Student(<span class="hljs-string">&apos;lisi&apos;</span>,<span class="hljs-number">95</span>)
<span class="hljs-keyword">del</span> s
input(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&#x5185;&#x5BB9;&apos;</span>)
</code></pre>
<h3 id="3str&#x65B9;&#x6CD5;">3.__str__&#x65B9;&#x6CD5;</h3>
<p><code>__str__</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x7684;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;&#xFF0C;&#x4F7F;&#x7528;<code>print()</code>&#x51FD;&#x6570;&#x6253;&#x5370;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5176;&#x5B9E;&#x8C03;&#x7528;&#x7684;&#x5C31;&#x662F;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<code>__str__</code>&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,color)</span>:</span>
        self.name = name
        self.color = color

tom = Cat(<span class="hljs-string">&apos;Tom&apos;</span>,<span class="hljs-string">&apos;white&apos;</span>)

<span class="hljs-comment"># &#x4F7F;&#x7528; print &#x65B9;&#x6CD5;&#x6253;&#x5370;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x7684; __str__ &#x65B9;&#x6CD5;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F1A;&#x6253;&#x5370;&#x7C7B;&#x540D;&#x548C;&#x5BF9;&#x8C61;&#x7684;&#x5730;&#x5740;&#x540D;</span>
print(tom)   <span class="hljs-comment"># &lt;__main__.Cat object at 0x0000021BE3B9C940&gt;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x60F3;&#x8981;&#x4FEE;&#x6539;&#x5BF9;&#x8C61;&#x7684;&#x8F93;&#x51FA;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x5199; <code>__str__</code> &#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span>
        self.name = name
        self.age = age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&#x54C8;&#x54C8;&apos;</span>

p  = Person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>,<span class="hljs-number">18</span>)
print(p)   <span class="hljs-comment"># &#x54C8;&#x54C8;  &#x6253;&#x5370;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x7684; __str__ &#x65B9;&#x6CD5;</span>
</code></pre>
<p>&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x6253;&#x5370;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5217;&#x51FA;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,score)</span>:</span>
        self.name = name
        self.score = score
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&#x59D3;&#x540D;&#x662F;:{},&#x6210;&#x7EE9;&#x662F;{}&#x5206;&apos;</span>.format(self.name,self.score)

s = Student(<span class="hljs-string">&apos;lisi&apos;</span>,<span class="hljs-number">95</span>)
print(s)   <span class="hljs-comment"># &#x59D3;&#x540D;&#x662F;:lisi,&#x6210;&#x7EE9;&#x662F;95&#x5206;</span>
</code></pre>
<h3 id="4-repr&#x65B9;&#x6CD5;">4. __repr__&#x65B9;&#x6CD5;</h3>
<p><code>__repr__</code>&#x65B9;&#x6CD5;&#x548C;<code>__str__</code>&#x65B9;&#x6CD5;&#x529F;&#x80FD;&#x7C7B;&#x4F3C;&#xFF0C;&#x90FD;&#x662F;&#x7528;&#x6765;&#x4FEE;&#x6539;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x9ED8;&#x8BA4;&#x6253;&#x5370;&#x5185;&#x5BB9;&#x3002;&#x5728;&#x6253;&#x5370;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x91CD;&#x5199;<code>__str__</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x4F1A;&#x81EA;&#x52A8;&#x6765;&#x67E5;&#x627E;<code>__repr__</code>&#x65B9;&#x6CD5;&#x3002;&#x5982;&#x679C;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x6CA1;&#x6709;&#xFF0C;&#x4F1A;&#x76F4;&#x63A5;&#x6253;&#x5370;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5185;&#x5B58;&#x5730;&#x5740;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, score)</span>:</span>
        self.name = name
        self.score = score

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;helllo&apos;</span>


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hi&apos;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;good&apos;</span>


s = Student(<span class="hljs-string">&apos;lisi&apos;</span>, <span class="hljs-number">95</span>)
print(s)  <span class="hljs-comment"># hello</span>

p = Person()
print(p)  <span class="hljs-comment"># good</span>
</code></pre>
<h3 id="5-call&#x65B9;&#x6CD5;">5. __call__&#x65B9;&#x6CD5;</h3>
<p>&#x5BF9;&#x8C61;&#x540E;&#x9762;&#x52A0;&#x62EC;&#x53F7;&#xFF0C;&#x89E6;&#x53D1;&#x6267;&#x884C;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">pass</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span><span class="hljs-params">(self, *args, **kwargs)</span>:</span>
        print(<span class="hljs-string">&apos;__call__&apos;</span>)


obj = Foo()  <span class="hljs-comment"># &#x6267;&#x884C; __init__</span>
obj()  <span class="hljs-comment"># &#x6267;&#x884C; __call__</span>
</code></pre>
<h3 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h3>
<ol>
<li>&#x5F53;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;<code>__init__</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5F53;&#x5220;&#x9664;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;<code>__del__</code>&#x65B9;&#x6CD5;&#x3002;</li>
<li>&#x4F7F;&#x7528;<code>__str__</code>&#x548C;<code>__repr__</code>&#x65B9;&#x6CD5;&#xFF0C;&#x90FD;&#x4F1A;&#x4FEE;&#x6539;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x6210;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;<code>__str__</code>&#x65B9;&#x6CD5;&#x7684;&#x7ED3;&#x679C;&#x66F4;&#x52A0;&#x5728;&#x610F;&#x53EF;&#x8BFB;&#x6027;&#xFF0C;&#x800C;<code>__repr__</code>&#x65B9;&#x6CD5;&#x7684;&#x7ED3;&#x679C;&#x66F4;&#x52A0;&#x5728;&#x610F;&#x6B63;&#x786E;&#x6027;(&#x4F8B;&#x5982;:datetime&#x6A21;&#x5757;&#x91CC;&#x7684;datetime&#x7C7B;)</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter03.html" class="navigation navigation-prev " aria-label="Previous page: 面向对象基本语法"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter05.html" class="navigation navigation-next " aria-label="Next page: 魔法方法2"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
